const app = getApp()
import { getAddressList, getAskSampleList, submitOrder, getAskSample } from '../../../../api/index'

Page({
  data: {
    cartIds: '',
    addressId: '',
    currentAddress: null,
    list: [],
    allCount: 0,
    form: '',
    specKey: '',
    materialId: '',
    num: ''
  },
  async submit () {
    try {
      if (!this.data.currentAddress || !this.data.currentAddress.id) {
        wx.showToast({ title: '请先选择发货地址！', icon: 'none' })
        return
      }
      let orderList = []
      this.data.list.forEach(item => {
        orderList.push({
          supplierId: item.supplierId,
          remark: item.remark,
          detail: item.materialArray
        })
      })
      let query = {
        orderList: orderList,
        addressId: this.data.currentAddress.id,
        cardId: this.data.cartIds
      }
      await submitOrder({ ...query })
      wx.showToast({ title: '订单提交成功！', icon: 'none' })
      wx.$nav.navigateBack()
    } catch (err) {
      console.log(err)
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  changeInput ({ currentTarget: { dataset: { item, key, index } }, detail: { value } }) {
    let setKey = `list[${index}].${key}`
    this.setData({ [setKey]: value })
  },
  goAddress () {
    if (this.data.form === 'now') {
      wx.$nav.redirectTo(`/pages/account/address/index/index?formPage=cart&form=${this.data.form}&specKey=${this.data.specKey}&materialId=${this.data.materialId}&num=${this.data.num}`)
    } else {
      wx.$nav.redirectTo(`/pages/account/address/index/index?formPage=cart&form=${this.data.form}&ids=${this.data.cartIds}`)
    }
  },
  async getSampleList () {
    try {
      const { ResultSet } = await getAskSampleList({ cardIdList: this.data.cartIds })
      ResultSet.userData.cartArray.forEach(item => {
        item.remark = ''
      })
      let sections = []
      ResultSet.userData.cartArray.forEach(item => {
        let rowSections = item.materialArray.filter(row => row.num)
        sections.push(...rowSections)
      })
      let allCount = sections.map(item => { return item.num }).reduce((n,m) => n + m)
      this.setData({ list: ResultSet.userData.cartArray, allCount })
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async getAddressList () {
    try {
      const { ResultSet } = await getAddressList()
      if (this.data.addressId) {
        this.data.currentAddress = ResultSet.userData.data.find(item => item.id === this.data.addressId)
      } else {
        this.data.currentAddress = ResultSet.userData.data.find(item => item.isDefault)
      }
      if (this.data.currentAddress && this.data.currentAddress.id) {
        this.setData({ currentAddress: this.data.currentAddress })
      } else {
        this.setData({ currentAddress: ResultSet.userData.data[0] })
      }
    } catch (err) {
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async getAskSample () {
    try {
      const { ResultSet } = await getAskSample({ materialId: this.data.materialId, specKey: this.data.specKey, num: this.data.num })
      console.log(ResultSet)
      let cartArray = [ResultSet.userData.orderList]
      cartArray.forEach(item => {
        item.remark = ''
      })
      let sections = []
      cartArray.forEach(item => {
        let rowSections = item.materialArray.filter(row => row.num)
        sections.push(...rowSections)
      })
      let allCount = sections.map(item => { return item.num }).reduce((n,m) => n + m)
      this.setData({ list: cartArray, allCount })
    } catch (err) {
      console.log(err)
      wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'none' })
    }
  },
  async onLoad ({ form, ids, id, specKey, materialId, num }) {
    if (id) this.setData({ addressId: parseInt(id)})
    await this.getAddressList()
    if (form === 'now') {
      this.setData({ form, specKey, materialId, num })
      this.getAskSample()
    } else {
      this.setData({ cartIds: ids, form })
      this.getSampleList()
    }
  }
})